package com.ebt.m.widget.tablescroll.scroll;

import android.content.Context;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ebt.m.R;
import com.ebt.m.utils.NumberHelper;

import java.util.ArrayList;
import java.util.List;


public class AdapterBenefitList extends BaseAdapter {
    private Context context;
    private List<RowBenefit> list;
    private RelativeLayout rl;
    public static final int tablecelltextsize = 12;
    public static final int tablecellwidth = 80;
    public static final int tablecellheight = 50;

    public AdapterBenefitList(Context context, List<RowBenefit> list, RelativeLayout rl) {
        this.context = context;
        this.list = list;
        this.rl = rl;
    }


    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int location) {
        return list.get(location);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parentView) {
        RowBenefit rowBenefit = list.get(position);
        ViewHolder viewholder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.layout_table_scroll_item, null, false);
            viewholder = new ViewHolder();
            LinearLayout ll_benifit_titles = (LinearLayout) convertView
                    .findViewById(R.id.ll_benifit_titles);
            for (int i = 0, size = rowBenefit.getBenefitsPerAge().size(); i < size; i++) {
                TextView textView1 = new TextView(context);
                textView1.setGravity(Gravity.CENTER);
                textView1.setWidth(dip2px(context, tablecellwidth));
                textView1.setHeight(dip2px(context, AdapterBenefitList.tablecellheight));
                textView1.setTextSize(tablecelltextsize);
                textView1.setTextColor(context.getResources().getColor(R.color.table_data_textcolor_modify));
                ll_benifit_titles.addView(textView1);
                viewholder.textViewList.add(textView1);

                /** 添加垂直分隔线 **/
                if (i < size - 1) {
                    TextView divlerView = new TextView(context);
                    divlerView.setWidth(dip2px(context, 0.5f));
                    divlerView.setHeight(dip2px(context, AdapterBenefitList.tablecellheight));
                    divlerView.setBackgroundColor(context.getResources().getColor(R.color.table_column_divler));
                    ll_benifit_titles.addView(divlerView);
                }
            }

            HScrollViewWithObserver scrollView1 = (HScrollViewWithObserver) convertView
                    .findViewById(R.id.hsrollviewTable);

            viewholder.scrollView = scrollView1;
            viewholder.tv1 = (TextView) convertView
                    .findViewById(R.id.tvRowAge);
            viewholder.tv1.setTextColor(context.getResources().getColor(R.color.table_data_textcolor_modify));

            HScrollViewWithObserver headSrcrollView =
                    (HScrollViewWithObserver) rl.findViewById(R.id.hsrollviewTable);
            headSrcrollView.AddOnScrollChangedListener(new OnScrollChangedListenerImp(scrollView1));

            convertView.setTag(viewholder);

        } else {
            viewholder = (ViewHolder) convertView.getTag();
        }
        viewholder.tv1.setText(rowBenefit.getAge() + "");
        List<Double> benefitsPerAge = rowBenefit.getBenefitsPerAge();
        for (int i = 0, size = viewholder.textViewList.size(); i < size; i++) {
            viewholder.textViewList.get(i).setText(NumberHelper.formatMoney(benefitsPerAge.get(i)));
        }
        return convertView;
    }

    class OnScrollChangedListenerImp implements HScrollViewWithObserver.OnScrollChangedListener {
        HScrollViewWithObserver mScrollViewArg;

        public OnScrollChangedListenerImp(HScrollViewWithObserver scrollViewar) {
            mScrollViewArg = scrollViewar;
        }

        @Override
        public void onScrollChanged(int l, int t, int oldl, int oldt) {
            mScrollViewArg.smoothScrollTo(l, t);
        }
    }

    ;

    private class ViewHolder {
        TextView tv1;
        List<TextView> textViewList = new ArrayList<TextView>();
        HorizontalScrollView scrollView;
    }

    public int dip2px(Context context, float dpValue) {
        DisplayMetrics metrics = context.getResources().getDisplayMetrics();
        return (int) (dpValue * metrics.density + 0.5f);
    }
}
